#pragma once

#include <thread>
#include <mutex>
#include <queue>

#include "tas_spinlock.h"

template <typename T>
class coarse_grained_heap {
public:
    coarse_grained_heap(const size_t /* capacity */)
    {}

    void insert(const T& e) {
        std::lock_guard<mutex_t> lock(mtx_);
        heap_.push(std::move(e));
    }

    bool extract_min(T& e) {
        std::lock_guard<mutex_t> lock(mtx_);
        if (!heap_.empty()) {
            e = std::move(heap_.top());
            heap_.pop();
            return true;
        } else {
            return false;
        }
    }

private:
    using heap_t = std::priority_queue<T, std::vector<T>, std::greater<T>>;
    using mutex_t = std::mutex;
    //using mutex_t = tas_spinlock;

    heap_t heap_;
    mutex_t mtx_;
};

